﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Mengsk.ProxyDelivery.Dao.NETDao
{
    public class Parameter
    {
        public const string TYPE_MAP = "map";
        public const string TYPE_LIST = "list";
        public const string TYPE_ARRAY = "array";
        public const string TYPE_STRING = "string";
        public const string TYPE_INT = "int";
        public const string TYPE_LONG = "long";
        public const string TYPE_DATETIME = "datetime";

        public string Name { get; set; }

        public object Value { get; set; }

        public List<ParameterEntry> Entries { get; private set; }

        public Parameter()
        {
            this.Entries = new List<ParameterEntry>();
        }

        private string GetObjectType(object value)
        {
            if (value.GetType() == typeof(int) || value.GetType() == typeof(short) || value.GetType() == typeof(byte))
            {
                return TYPE_INT;
            }

            if (value.GetType() == typeof(long))
            {
                return TYPE_LONG;
            }

            if (value.GetType().IsArray)
            {
                return TYPE_ARRAY;
            }

            Type[] types = value.GetType().GetInterfaces();
            if (types.Any(obj => obj.Name.Contains("IDictionary")))
            {
                return TYPE_MAP;
            }

            if (types.Any(obj => obj == typeof(System.Collections.IEnumerable)))
            {
                return TYPE_LIST;
            }
            throw new Exception("无法认识的类型:" + value.GetType().FullName);
        }

        public XElement ToXElement()
        {
            String type = this.GetObjectType(Value);
            if (type == TYPE_STRING || type == TYPE_INT || type == TYPE_LONG)
            {
                return new XElement("parameter", new XAttribute("name", Name), new XAttribute("type", type), Value);
            }

            if (type == TYPE_DATETIME)
            {
                return new XElement("parameter", new XAttribute("name", Name), new XAttribute("type", type), ((DateTime)Value).ToString("yyyy-MM-dd HH:mm:ss"));
            }

            XElement xele = new XElement("parameter", new XAttribute("name", Name), new XAttribute("type", type));
            foreach (var pEntry in this.Entries)
            {
                string pEntryType = this.GetObjectType(pEntry.Value);
                if (pEntryType == TYPE_DATETIME)
                {
                    return new XElement("parameter", new XAttribute("name", Name), new XAttribute("type", pEntryType), ((DateTime)Value).ToString("yyyy-MM-dd HH:mm:ss"));
                }
                else
                {
                    xele.Add(new XElement("entry", new XAttribute("name", pEntry.Name), new XAttribute("type", pEntryType), pEntry.Value));
                }
            }
            return xele;
        }

    }
}
